package com.fht.spring_boot_study.model.test.dao;

import com.fht.spring_boot_study.model.common.entity.SearchBean;
import com.fht.spring_boot_study.model.test.entity.City;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface CityDao {
    @Insert("insert into test_city(city_name,local_city_name,country_id,district,population,date_modified,date_created) " +
            "values (#{cityName},#{localCityName},#{countryId},#{district},#{population},#{dateModified},#{dateCreated})" )


    @Options(useGeneratedKeys = true, keyColumn = "city_id",keyProperty = "cityId")
    void insertCity(City city);

    @Update("update test_city set city_name=#{cityName},local_city_name=#{localCityName} where city_id=#{cityId}")
    void updateCity(City city);

    @Delete("delete from test_city where city_id=#{cityId}")
    void deleteCityByCityId(int cityId);
    @Select("select * from test_city tc left join test_country tco " +
            "on tc.country_id=tco.country_id where tc.city_id=#{cityId}")
    City getCityByCityId(int cityId);

    @Select("select * from test_city where country_id=#{countryId}")
    List<City> getCitiesByCountryId(int countryId);

    @Select("<script>" +
            "select * from test_city "
            + "<where> "
            + "<if test='keyWord != \"\" and keyWord != null'>"
            + " and (city_name like '%${keyWord}%' or district like '%${keyWord}%') "
            + "</if>"
            + "</where>"
            + "<choose>"
            + "<when test='orderBy != \"\" and orderBy != null'>"
            + " order by ${orderBy} ${direction}"
            + "</when>"
            + "<otherwise>"
            + " order by city_id desc"
            + "</otherwise>"
            + "</choose>"
            + "</script>")
    List<City> getCitiesBySearchBean(SearchBean searchBean);
}
